package intranet.controllers;
import org.springframework.web.servlet.ModelAndView;
import intranet.models.*;
import intranet.tools.*;

import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class CourseController extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/* IsNumber Function */
	public static boolean IsNumber(String num) {
        try {
            Integer.parseInt(num);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
	
	@RequestMapping("/courses.html")
	public ModelAndView Courses(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){
			CourseModel Course=new CourseModel();
			ResultSet rstop=Course.SelectTopCourse();
			ArrayList rstops=this.resultSetToArrayList(rstop);
			ResultSet rsother=Course.SelectOtherCourse();
			ArrayList rsothers=this.resultSetToArrayList(rsother);
		
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/courses.jsp");
			// Passing File Category
			FileCategoryModel mycat=new FileCategoryModel();
			ResultSet rscat=mycat.SelectFileCategory(0);
			ArrayList cat=this.resultSetToArrayList(rscat);
			mv.addObject("categories",cat);
			// Passing Most Viewed & Other Videos
			mv.addObject("topcourses", rstops);
			mv.addObject("othercourses", rsothers);
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	@RequestMapping("/view_course.html")
	public ModelAndView ViewCourse(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){
			String id= request.getParameter("course_id");
			id=(id==null || IsNumber(id)==false)?"0":id;
			CourseModel Course=new CourseModel();
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/view_course.jsp");
			
			/* SELECT COURSE */
			ResultSet rsCourse=Course.SelectCourse(Integer.parseInt(id));
			ArrayList arrCourse=this.resultSetToArrayList(rsCourse);
			mv.addObject("course", arrCourse);
			
			/* SELECT INSTRUCTION */
			ResultSet rsInst=Course.SelectInstructionFile(Integer.parseInt(id));
			ArrayList arrInst=this.resultSetToArrayList(rsInst);
			mv.addObject("instruction", arrInst);
			
			/* SELECT VIDEO */
			ResultSet rsVideo=Course.SelectCourseFileByType(Integer.parseInt(id), "Video");
			ArrayList arrVideo=this.resultSetToArrayList(rsVideo);
			mv.addObject("videos", arrVideo);
			
			/* SELECT AUDIO */
			ResultSet rsAudio=Course.SelectCourseFileByType(Integer.parseInt(id), "Audio");
			ArrayList arrAudio=this.resultSetToArrayList(rsAudio);
			mv.addObject("audios", arrAudio);
			
			/* SELECT Slide Presentation */
			ResultSet rsSlide=Course.SelectCourseFileByType(Integer.parseInt(id), "Slide Presentation");
			ArrayList arrSlide=this.resultSetToArrayList(rsSlide);
			mv.addObject("slides", arrSlide);
			
			/* SELECT Other */
			ResultSet rsOther=Course.SelectCourseFileByType(Integer.parseInt(id), "Other");
			ArrayList arrOther=this.resultSetToArrayList(rsOther);
			mv.addObject("others", arrOther);
			
			WebConfig wconf=new WebConfig();
			mv.addObject("ServerURL",wconf.getURL());
		
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	
	@RequestMapping("/play_course_video.html")
	public ModelAndView PlayCourseVideo(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){	
			String id= request.getParameter("video_id");
			id=(id==null || IsNumber(id)==false)?"0":id;
			CourseModel Video=new CourseModel();
			ResultSet playitem=Video.CourseFile(Integer.parseInt(id));
			ArrayList item=this.resultSetToArrayList(playitem);
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/course_video_player.jsp");
			// Passing More Videos
			mv.addObject("item", item);
			
			WebConfig wconf=new WebConfig();
			mv.addObject("ServerURL",wconf.getURL());
	}else{
		response.sendRedirect("login.html");
	}
	return mv;
	}
	
	@RequestMapping("/play_course_audio.html")
	public ModelAndView PlayCourseAudio(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){		
			String id= request.getParameter("audio_id");
			id=(id==null || IsNumber(id)==false)?"0":id;
			CourseModel Video=new CourseModel();
			ResultSet playitem=Video.CourseFile(Integer.parseInt(id));
			ArrayList item=this.resultSetToArrayList(playitem);
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/course_audio_player.jsp");
			// Passing More Videos
			mv.addObject("item", item);
			
			WebConfig wconf=new WebConfig();
			mv.addObject("ServerURL",wconf.getURL());
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	@RequestMapping("/view_course_detail.html")
	public ModelAndView ViewCourseDetail(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws Exception {
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){		
			String id= request.getParameter("view_id");
			id=(id==null || IsNumber(id)==false)?"0":id;
			CourseModel Video=new CourseModel();
			ResultSet playitem=Video.CourseFile(Integer.parseInt(id));
			ArrayList item=this.resultSetToArrayList(playitem);
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/course_file_detail.jsp");
			// Passing More Videos
			mv.addObject("item", item);
			
			WebConfig wconf=new WebConfig();
			mv.addObject("ServerURL",wconf.getURL());
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	@RequestMapping("/more_courses.html")
	public ModelAndView MoreCourses(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){		
			String page= request.getParameter("page");
			CourseModel Course=new CourseModel();
			ResultSet rsmore=Course.SelectMoreCourse();
			ArrayList arrmore=this.resultSetToArrayList(rsmore);
			int RowCount=arrmore.size();
			page=(page==null || IsNumber(page)==false)?"1":page;
			// Pagination Link Elements
			int PageSize=8;
			int PageCount=((RowCount%PageSize)>0)?Math.round(RowCount/PageSize)+1:Math.round(RowCount/PageSize);
			page=(Integer.parseInt(page)<=0 ||Integer.parseInt(page)>PageCount)?"1":page;
			int StartingRow=(Integer.parseInt(page)-1)*PageSize;
			int EndingRow=(Integer.parseInt(page)*PageSize)-1;
			mv.addObject("page", page);
			mv.addObject("PageCount", PageCount);
			mv.addObject("StartingRow", StartingRow);
			mv.addObject("EndingRow", EndingRow);
			
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/more_courses.jsp");
			// Passing File Category
			FileCategoryModel mycat=new FileCategoryModel();
			ResultSet rscat=mycat.SelectFileCategory(0);
			ArrayList cat=this.resultSetToArrayList(rscat);
			mv.addObject("categories",cat);
			// Passing Most Viewed & Other Videos
			mv.addObject("courses", arrmore);
		}else{
			response.sendRedirect("login.html");
		}
	return mv;
		
	}
	
	@RequestMapping("/search_courses.html")
	public ModelAndView SearchCourses(HttpServletRequest request, HttpServletResponse response,HttpSession session) throws SQLException,ServletException, IOException{
		ModelAndView mv=new ModelAndView("index");
		if(session.getAttribute("user_id")!=null){		
			String cat_id=request.getParameter("txtcat");
			String keywords=request.getParameter("txtkeyword");
			CourseModel Course=new CourseModel();
			ResultSet rsresult=Course.SearchCourse(Integer.parseInt(cat_id), keywords);
			ArrayList results=this.resultSetToArrayList(rsresult);
			// Passing Page Included
			mv.addObject("includethispage", "learning_area/course/search_courses.jsp");
			// Passing Search Data
			mv.addObject("cat_id", cat_id);
			mv.addObject("keywords", keywords);
			// Passing File Category
			FileCategoryModel mycat=new FileCategoryModel();
			ResultSet rscat=mycat.SelectFileCategory(0);
			ArrayList cat=this.resultSetToArrayList(rscat);
			mv.addObject("categories",cat);
			// Passing More Videos
			mv.addObject("courses", results);
		}else{
			response.sendRedirect("login.html");
		}
		return mv;
	}
	
	// RESULTSET TO ARRAYLIST
	   public ArrayList resultSetToArrayList(ResultSet rs) throws SQLException{
		    ResultSetMetaData md = rs.getMetaData();
		    int columns = md.getColumnCount();
		    ArrayList results = new ArrayList();
		    while (rs.next()){
		    HashMap row = new HashMap();
		    results.add(row);
		    for(int i=1; i<=columns; i++){
		    row.put(md.getColumnName(i),rs.getObject(i));
		    }
		    }
		    return results;

	    }
}